using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.ServiceModel;
using GZ.Common.Data;
using System.Configuration;
using Newtonsoft.Json;
using GZ.Common.Code;
using GZ.DB.Repository.wcs_yb;
using GZ.DB.Entity.wcs_yb;
using GZ.DB.IRepository.wcs_yb;
using static GZ.Projects.WCS_SVC.WMSHttpEntity;
using System.Net;
using System.Net.NetworkInformation;
namespace GZ.Projects.WCS_SVC
{
public class AutoScanApp
{
#region    [自定义类][20220817143019850][AutoScanApp]
								public bool AddMainMissionList(MainMissionEntity entity, out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Insert(entity);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
	
}


public bool UpdateMainMissionTask(MainMissionEntity entity, out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
	
}



public bool UpdateAgvAndInsertSysTask(AgvTaskEntity entity, SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			if (sysEntity != null)
			{
				db.Insert(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}

public bool UpdateMainAndSysTask(MainMissionEntity entity, SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			if (sysEntity != null)
			{
				db.Update(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}

public bool UpdateMainAndAgvTaskAndInsertSysTask(MainMissionEntity entity, SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			if (sysEntity != null)
			{
				db.Update(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}
public bool UpdateMainAndAgvTaskAndInsertSysTask(MainMissionEntity mainEntity, AgvTaskEntity agvEntity,SystemSynchronizationEntity sysEntity,out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (mainEntity != null)
			{
				db.Update(mainEntity);
			}
			if (agvEntity != null)
			{
				db.Update(agvEntity);
			}
			if (sysEntity != null)
			{
				db.Insert(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}


public bool UpdateMainAndSysTaskAndInsertAgvTask(MainMissionEntity mainEntity,SystemSynchronizationEntity sysEntity, AgvTaskEntity agvEntity,out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (mainEntity != null)
			{
				db.Update(mainEntity);
			}
			if (sysEntity != null)
			{
				db.Update(sysEntity);
			}
			if (agvEntity != null)
			{
				db.Insert(agvEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}
public bool UpdateStackAndSysTask(StackerTaskEntity entity, SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			if (sysEntity != null)
			{
				db.Update(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}
public bool UpdateAgvTask(AgvTaskEntity entity, out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
	
}

public bool UpdateStackerAndMainTask(StackerTaskEntity entity,MainMissionEntity mainList,  out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if(entity!=null)
			{
				db.Update(entity);
			}
			if(mainList!=null)
			{
				db.Update(mainList);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
			
}

public bool UpdateAgvAndMainTask(AgvTaskEntity entity,MainMissionEntity mainList,  out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if(entity!=null)
			{
				db.Update(entity);
			}
			if(mainList!=null)
			{
				db.Update(mainList);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
			
}

public bool UpdateMainAndInsertSysTask(MainMissionEntity entity, SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			if (sysEntity != null)
			{
				db.Insert(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}
public bool InsertSysTask(SystemSynchronizationEntity sysEntity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (sysEntity != null)
			{
				db.Insert(sysEntity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}

public bool updateMainTask(MainMissionEntity entity, out string err)
{
	err = "";
	int dbResult = 0;
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if (entity != null)
			{
				db.Update(entity);
			}
			dbResult = db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return dbResult >= 1;
}

public bool UpdateStackerTwoAndMainTask(StackerTaskEntity entity1, StackerTaskEntity entity2,MainMissionEntity mainEntity,  out string err)
{
	err = "";
	try
	{
		using (var db = new RepositoryBase().BeginTrans())
		{
			if(entity1 != null)
			{
				db.Update(entity1);
			}
			if(entity2 != null)
			{
				db.Update(entity2);
			}
			if(mainEntity != null)
			{
				db.Update(mainEntity);
			}
			db.Commit();
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}

	return true;
	
}
#endregion [自定义类][20220817143019850][AutoScanApp]
}
public class ToHandleAgvDifStatus
{
#region    [自定义类][20250519200049949][ToHandleAgvDifStatus]
				private AutoScanApp _auto = new AutoScanApp();
		/// <summary>
		/// agv任务开始
		/// </summary>
	public bool StartAgvCallBack(AgvTaskEntity entity,  out string err)
{	
	err = "";
	try
	{
		//需要校验我此时的任务状态是不是创建状态
		if (entity.task_state != "Create")
		{
			err = "任务状态不是创建状态";
			return false;
		}
		entity.task_state = "Start";
		entity.TaskStartTime = DateTime.Now;
		entity.LastUpdatedTime = DateTime.Now;
		//发送redis
		//WCS_INFINISHSENT_{station} 放货完成
		//WCS_OUTFINISHSENT_{station} 取货完成
		bool flagStart = _auto.UpdateAgvTask(entity, out err);
		if (!flagStart)
		{
			return false;
		}
		if (string.IsNullOrEmpty(err))
		{
			err = "成功";
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return true;
}
	/// <summary>
	/// agv走出储位
	/// </summary>
	public bool OutbinAgvCallBack(AgvTaskEntity entity,  out string err)
	{	
		err = "";
		try
		{
			entity.task_state = "Doing";
			entity.TaskStartTime = DateTime.Now;
			entity.LastUpdatedTime = DateTime.Now;
			bool flagStart = _auto.UpdateAgvTask(entity, out err);
			if (!flagStart)
			{
				return false;
			}
			if (string.IsNullOrEmpty(err))
			{
				err = "成功";
			}
		}
		catch (Exception ex)
		{
			err = ex.ToString();
			return false;
		}
		return true;
	}
	/// <summary>
	/// agv任务结束
	/// </summary>
public bool EndAgvCallBack(AgvTaskEntity entity, out string err)
{
	err = "";
	try
	{
		
		entity.task_state = "End";
		entity.TaskEndTime = DateTime.Now;
		entity.LastUpdatedTime = DateTime.Now;
		bool flagEnd = _auto.UpdateAgvTask(entity, out err);
		if (!flagEnd)
		{
			return false;
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return true;
	
}
public bool CancelAgvCallBack(AgvTaskEntity entity, out string err)
{
	err = "成功";
	try
	{
		entity.task_state = "Cancel";
		entity.TaskEndTime = DateTime.Now;
		entity.LastUpdatedTime = DateTime.Now;
		bool flagCancel = _auto.UpdateAgvTask(entity, out err);
		if (!flagCancel)
		{
			return false;
		}
	}
	catch (Exception ex)
	{
		err = ex.ToString();
		return false;
	}
	return true;
}
#endregion [自定义类][20250519200049949][ToHandleAgvDifStatus]
}
}